package com.corpgovernment.common.common;

import com.corpgovernment.common.holder.TenantContextHolder;
import com.ctrip.corp.obt.generic.utils.JsonUtils;
import com.corpgovernment.common.utils.LogSplicingUtils;
import com.ctrip.corp.obt.generic.utils.StringUtils;
import com.fasterxml.jackson.databind.JsonNode;
import lombok.Data;

import java.util.Objects;

@Data
public class CommonSupplierUrlBo {
    /**
     * 员工开卡
     */
    private UrlInfo employeeOpenCard;
    /**
     * 获取开卡信息
     */
    private UrlInfo getOpenCardInfo;
    /**
     * 获取绑定账号验证码
     */
    private UrlInfo getBindVerifyCode;
    /**
     * 绑定携程账号
     */
    private UrlInfo bindAccount;
    /**
     * 发送邮件配置
     */
    private UrlInfo sendEmail;

    @Data
    public static class UrlInfo{
        private String name;
        private String url;
        private String corpId;
        private String subAccountName;
        private String supplierCode;
    }

    public static CommonSupplierUrlBo getCommonSupplierUrl(String json, StringBuilder logContext){
        if (Objects.isNull(logContext)){
            logContext = new StringBuilder();
        }
        LogSplicingUtils.addLogContext(logContext, "commonUrl：%s", json);
        if (StringUtils.isBlank(json)){
            LogSplicingUtils.addLogContext(logContext, "apollo未配置推送地址");
            return null;
        }
        //转换为note节点
        JsonNode jsonNode = JsonUtils.getJsonNode(json);
        //获取租户ID
        String tenantId = TenantContextHolder.getTenantId();
        LogSplicingUtils.addLogContext(logContext, "tenantId：%s", tenantId);

        //根据租户ID获取该租户配置的推送配置
        JsonNode path = jsonNode.findPath(tenantId);
        if (StringUtils.isBlank(path + "")){
            LogSplicingUtils.addLogContext(logContext, "apollo未配置该租户地址");
            return null;
        }
        //转换为实体类
        CommonSupplierUrlBo commonSupplierUrlBo = JsonUtils.parse(path.toString(), CommonSupplierUrlBo.class);
        LogSplicingUtils.addLogContext(logContext, "commonSupplierUrlBo：%s", JsonUtils.toJsonString(commonSupplierUrlBo));
        return commonSupplierUrlBo;
    }
}
